// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   p.java

package com.intellij.ide.license.impl;

import com.intellij.concurrency.JobScheduler;
import com.intellij.ide.license.LicenseException;
import com.intellij.ide.license.LicenseFactory;
import com.intellij.ide.license.s;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.impl.ApplicationInfoImpl;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ui.UIUtil;
import com.jetbrains.licenseService.responses.*;
import com.jetbrains.licenseService.util.ClientUtil;
import com.jetbrains.licenseService.util.DecodeException;
import com.jetbrains.licenseService.util.VerificationFailedException;

import javax.swing.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.prefs.Preferences;

// Referenced classes of package com.intellij.ide.a.f:
//            m, a, z, w, 
//            x, b, bb

public class JetprofileSource extends GeneralLicenseSource implements s
{
    private class c_ implements Runnable {

        private final JetProfileLicenseData a;
        private final long b;
        final JetprofileSource this$0;

        public void run()
        {
            if(p.q.getAndSet(true))
                return;
            JetProfileLicenseData w1 = JetprofileSource.this.p();
            if(w1 == null || w1.u().getAssetId() != a.u().getAssetId())
            {
                p.q.set(false);
                return;
            }
            if(JetprofileSource.this.p && System.currentTimeMillis() - j() < b)
            {
                p.q.set(false);
                return;
            }
            JetprofileSource.this.p = true;
            boolean flag = false;
            String s1 = "";
            try
            {
                ValidateLicenseResponse validatelicenseresponse = JetprofileSource.this.b(a.s(), a.u().getAssetId());
                if(validatelicenseresponse.getResponseCode() == ResponseCode.OK)
                {
                    JetprofileSource.this.b(System.currentTimeMillis());
                    long l1 = validatelicenseresponse.getValidationPeriod();
                    if(l1 < 0L)
                        l1 = 0x36ee80L;
                    if(b != l1)
                    {
                        JetprofileSource.this.a(l1);
                        c(l1);
                    }
                } else
                {
                    flag = true;
                    s1 = validatelicenseresponse.getMessage();
                }
            }
            catch(IOException ioexception) { }
            catch(VerificationFailedException verificationfailedexception)
            {
                flag = true;
                s1 = verificationfailedexception.getMessage();
            }
            if(flag && e.compareAndSet(a, null))
            {
                r();
                l();
                p.d(s1, "License Validation");
                g.showLicenseUpdateMessage(new com.intellij.ide.license.a_() {

                    final c_ this$1;

                    public void proceed() {
                    }

                    public void cancel() {
                        GeneralLicenseManager.G();
                    }


                    {
                        this$1 = c_.this;
                        super();
                    }
                }
                        , JetprofileSource.this);
            }
            p.q.set(false);
            break MISSING_BLOCK_LABEL_331;
            Throwable throwable;
            throwable;
            p.c.debug("Unexpected error checking online license: ", throwable);
            p.q.set(false);
            break MISSING_BLOCK_LABEL_331;
            Exception exception;
            exception;
            p.q.set(false);
            throw exception;
        }

        public c_(JetProfileLicenseData w1, long l1)
        {
            if(w1 == null)
            {
                throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] {
                    "0", "com/intellij/ide/license/impl/JetprofileSource$ValidateLicenseTask", "<init>"
                }));
            } else
            {
                this$0 = JetprofileSource.this;
                super();
                a = w1;
                b = l1;
                return;
            }
        }
    }


    private static final Logger c = Logger.getInstance("#com.intellij.ide.license.impl.JetprofileSource");
    private static final long d = 0x36ee80L;
    private final AtomicReference e;
    private final long f;
    private final GeneralLicenseManager g;
    private final String h;
    private final int i;
    private final String j;
    private final LicenseFile k;
    private String l;
    private String m;
    private volatile ScheduledFuture n;
    private volatile List o;
    private volatile boolean p;
    private static AtomicBoolean q = new AtomicBoolean(false);
    static final boolean r = desiredAssertionStatus();

    public JetprofileSource(LicenseFactory o1, GeneralLicenseManager generalLicenseManager1, String s1, int i1, String s2)
    {
        if(o1 == null)
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] {
                "0", "com/intellij/ide/license/impl/JetprofileSource", "<init>"
            }));
        if(s1 == null)
        {
            throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] {
                "2", "com/intellij/ide/license/impl/JetprofileSource", "<init>"
            }));
        } else
        {
            super(o1);
            e = new AtomicReference(null);
            o = Collections.emptyList();
            p = false;
            g = generalLicenseManager1;
            h = s1;
            i = i1;
            f = com.intellij.licensecommon.g.l.a(ApplicationInfoImpl.getShadowInstance().getMajorReleaseBuildDate().getTime());
            j = s2;
            k = o1.e();
            return;
        }
    }

    public void a(LicenseCorruptException e1)
        throws LicenseSaveException
    {
        k.a(e1);
    }

    public void a()
        throws com.intellij.ide.license.LicenseException
    {
        try
        {
            String s1 = l;
            JetProfileLicenseData w1 = a(s1, m);
            b(w1);
            if(w1 != null)
            {
                a(w1);
                c(k());
            } else
            {
                throw new LicenseException("License Not Found", (new StringBuilder()).append("No suitable licenses associated with account ").append(s1).toString());
            }
        }
        catch(Exception exception)
        {
            b(((LicenseCorruptException) (null)));
            l = null;
            throw new LicenseException("JetProfile Error", exception.getMessage());
        }
        m = null;
        break MISSING_BLOCK_LABEL_110;
        Exception exception1;
        exception1;
        m = null;
        throw exception1;
    }

    private JetProfileLicenseData a(String s1, String s2)
        throws Exception
    {
        String s3 = c(s1, s2);
        ObtainLicenseResponse obtainlicenseresponse = a(s3);
        String s4 = obtainlicenseresponse.getMessage();
        if(obtainlicenseresponse.getResponseCode() != ResponseCode.OK)
        {
            StringBuilder stringbuilder = new StringBuilder();
            stringbuilder.append("Error fetching licenses for ").append(s1);
            if(!StringUtil.isEmptyOrSpaces(s4))
                stringbuilder.append(": ").append(s4);
            throw new Exception(stringbuilder.toString());
        }
        Collection collection = obtainlicenseresponse.getAssets();
        if(collection.isEmpty())
            return null;
        ArrayList arraylist = new ArrayList();
        EncodedAsset encodedasset;
        for(Iterator iterator = collection.iterator(); iterator.hasNext(); arraylist.add(a(s1, s3, encodedasset)))
            encodedasset = (EncodedAsset)iterator.next();

        o = arraylist;
        b(System.currentTimeMillis());
        a(obtainlicenseresponse.getValidationPeriod());
        return (JetProfileLicenseData)arraylist.get(0);
    }

    private JetProfileLicenseData a(String s1, String s2, EncodedAsset encodedasset)
        throws DecodeException
    {
        License license = encodedasset.decode();
        if(license.getUpgradeDueDate() == null)
            return new JetProfileLicenseData(s2, s1, i, encodedasset, license);
        else
            return new JetProfileSubscriptionLicenseData(s2, s1, i, encodedasset, license);
    }

    public void c()
    {
        if(!k.c())
        {
            l();
            return;
        }
        JetProfileLicenseData w1 = i();
        b(w1);
        if(w1 == null)
        {
            l();
            l = null;
        } else
        {
            l = w1.r();
            c(k());
        }
        m = null;
        break MISSING_BLOCK_LABEL_73;
        Exception exception;
        exception;
        m = null;
        throw exception;
    }

    public boolean e()
    {
        return true;
    }

    public void b()
        throws b
    {
        super.b();
    }

    private ObtainLicenseResponse a(String s1)
        throws IOException, VerificationFailedException
    {
        return ClientUtil.obtainLicenses(s1, h, licenseFactory.h() * 1000, j, f);
    }

    private ValidateLicenseResponse b(String s1, String s2)
        throws IOException, VerificationFailedException
    {
        return ClientUtil.validateLicense(s1, s2, h, licenseFactory.h() * 1000, j, f);
    }

    private String c(String s1, String s2)
        throws Exception
    {
        String s3;
        s3 = "JetProfile communication error";
        if(s1 == null)
        {
            s3 = "JetProfile login name not specified";
            break MISSING_BLOCK_LABEL_174;
        }
        ObtainUserIdResponse obtainuseridresponse = ClientUtil.obtainUserId(s1, s2 != null ? s2 : "", h);
        if(obtainuseridresponse.getResponseCode() != ResponseCode.OK) goto _L2; else goto _L1
_L1:
        String s4 = obtainuseridresponse.getUserId();
        s4;
        if(s4 == null)
            throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] {
                "com/intellij/ide/license/impl/JetprofileSource", "obtainUserId"
            }));
        return;
_L2:
        try
        {
            s3 = (new StringBuilder()).append("Authorization failed: ").append(obtainuseridresponse.getMessage()).toString();
        }
        catch(IOException ioexception)
        {
            s3 = (new StringBuilder()).append("JetProfile connection error: ").append(ioexception.getMessage()).toString();
        }
        catch(VerificationFailedException verificationfailedexception)
        {
            s3 = (new StringBuilder()).append("JetProfile communication error: ").append(verificationfailedexception.getMessage()).toString();
        }
        throw new Exception(s3);
    }

    private static void d(String s1, String s2)
    {
        UIUtil.invokeAndWaitIfNeeded(new Runnable(s1, s2) {

            final String val$message;
            final String val$title;

            public void run()
            {
                JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), message, title, 0);
            }

            
            {
                message = s1;
                title = s2;
                super();
            }
        }
);
    }

    public String g()
    {
        return l;
    }

    public String h()
    {
        return m;
    }

    public void b(String s1)
    {
        l = s1;
    }

    public void c(String s1)
    {
        m = s1;
    }

    private JetProfileLicenseData i()
    {
        byte abyte0[] = Preferences.userRoot().getByteArray(m(), null);
        if(abyte0 == null)
            return null;
        Properties properties = new Properties();
        try
        {
            properties.load(new ByteArrayInputStream(abyte0));
        }
        catch(IOException ioexception)
        {
            c.info(ioexception);
            return null;
        }
        String s1 = properties.getProperty("user.id", null);
        if(s1 == null)
            return null;
        String s2 = properties.getProperty("asset.data", null);
        if(s2 == null)
            return null;
        try
        {
            return a(properties.getProperty("user.login", null), s1, new EncodedAsset(s2));
        }
        catch(NumberFormatException numberformatexception)
        {
            c.info("Corrupted license data: ", numberformatexception);
        }
        catch(DecodeException decodeexception)
        {
            c.info("Corrupted license data: ", decodeexception);
        }
        return null;
    }

    private synchronized long j()
    {
        return Preferences.userRoot().getLong(n(), 0L);
    }

    private synchronized long k()
    {
        return Preferences.userRoot().getLong(o(), 0x36ee80L);
    }

    private synchronized void a(long l1)
    {
        Preferences.userRoot().putLong(o(), l1);
    }

    private synchronized void b(long l1)
    {
        Preferences.userRoot().putLong(n(), l1);
    }

    private void a(JetProfileLicenseData w1)
    {
        Properties properties;
        ByteArrayOutputStream bytearrayoutputstream;
        properties = new Properties();
        properties.setProperty("user.id", w1.s());
        String s1 = w1.r();
        if(s1 != null)
            properties.setProperty("user.login", s1);
        properties.setProperty("asset.data", w1.t().getData());
        bytearrayoutputstream = new ByteArrayOutputStream();
        properties.store(bytearrayoutputstream, "JetProfile asset information. Please do not alter this data");
        try
        {
            bytearrayoutputstream.close();
            Preferences.userRoot().putByteArray(m(), bytearrayoutputstream.toByteArray());
        }
        catch(IOException ioexception)
        {
            c.error(ioexception);
        }
        break MISSING_BLOCK_LABEL_191;
        IOException ioexception1;
        ioexception1;
        c.error(ioexception1);
        try
        {
            bytearrayoutputstream.close();
            Preferences.userRoot().putByteArray(m(), bytearrayoutputstream.toByteArray());
        }
        catch(IOException ioexception2)
        {
            c.error(ioexception2);
        }
        break MISSING_BLOCK_LABEL_191;
        Exception exception;
        exception;
        try
        {
            bytearrayoutputstream.close();
            Preferences.userRoot().putByteArray(m(), bytearrayoutputstream.toByteArray());
        }
        catch(IOException ioexception3)
        {
            c.error(ioexception3);
        }
        throw exception;
    }

    public void l()
    {
        Preferences preferences = Preferences.userRoot();
        preferences.remove(n());
        preferences.remove(m());
        preferences.remove(o());
    }

    protected String m()
    {
        return (new StringBuilder()).append("JetBrains.JetProfile.Asset.").append(h).append(".").append(licenseFactory.h()).toString();
    }

    private String n()
    {
        return (new StringBuilder()).append(m()).append(".Stamp").toString();
    }

    private String o()
    {
        return (new StringBuilder()).append(m()).append(".Period").toString();
    }

    public JetProfileLicenseData p()
    {
        return (JetProfileLicenseData)e.get();
    }

    public void b(LicenseCorruptException e1)
    {
        e.set((JetProfileLicenseData)e1);
        if(e1 == null)
            r();
    }

    public List q()
    {
        return o;
    }

    public void b(JetProfileLicenseData w1)
    {
        List list = o;
        boolean flag = false;
        Object obj = list.iterator();
        do
        {
            if(!((Iterator) (obj)).hasNext())
                break;
            JetProfileLicenseData w2 = (JetProfileLicenseData)((Iterator) (obj)).next();
            if(w1 != w2)
                continue;
            flag = true;
            break;
        } while(true);
        if(!r && !flag)
            throw new AssertionError("The license to switch to must be contained in the list of available licenses");
        obj = (JetProfileLicenseData)e.getAndSet(w1);
        if(w1 == null)
            r();
        else
        if(w1 != obj)
        {
            a(w1);
            c(k());
        }
    }

    private void r()
    {
        ScheduledFuture scheduledfuture = n;
        if(scheduledfuture != null)
        {
            n = null;
            scheduledfuture.cancel(true);
        }
    }

    private void c(final long validationPeriod)
    {
        r();
        long l1 = Math.min(validationPeriod / 4L, 0x36ee80L);
        n = JobScheduler.getScheduler().scheduleWithFixedDelay(new Runnable() {

            final long val$validationPeriod;
            final JetprofileSource this$0;

            public void run()
            {
                JetProfileLicenseData w1 = JetprofileSource.this.p();
                if(w1 != null)
                    ApplicationManager.getApplication().executeOnPooledThread(new c_(w1, validationPeriod));
            }

            
            {
                this$0 = JetprofileSource.this;
                validationPeriod = l1;
                super();
            }
        }
, p ? l1 : 0x927c0L, l1, TimeUnit.MILLISECONDS);
    }

    public volatile LicenseCorruptException d()
    {
        return p();
    }














}
